<template>
	<div class="sys-menu-container">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item :label="$t('message.pageConfiguration.menuName')">
					<el-input v-model="state.queryParams.title" :placeholder="$t('message.pageConfiguration.menuName')" clearable />
				</el-form-item>
				<el-form-item :label="$t('message.pageConfiguration.type')">
					<el-select v-model="state.queryParams.type" :placeholder="$t('message.pageConfiguration.type')" clearable>
						<el-option :label="$t('message.pageConfiguration.directory')" :value="1" />
						<el-option :label="$t('message.pageConfiguration.menu')" :value="2" />
						<el-option :label="$t('message.pageConfiguration.btn')" :value="3" />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'sysMenu:list'"> {{ $t('message.pageConfiguration.search') }} </el-button>
						<el-button icon="ele-Refresh" @click="resetQuery"> {{$t('message.pageConfiguration.reset')}} </el-button>
					</el-button-group>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="ele-Plus" @click="openAddMenu" v-auth="'sysMenu:add'"> {{ $t('message.pageConfiguration.add') }} </el-button>
				</el-form-item>
			</el-form>
		</el-card>

		<el-card class="full-table" shadow="hover" style="margin-top: 8px">
			<el-table :data="state.menuData" v-loading="state.loading" row-key="id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" border>
				<el-table-column :label="$t('message.pageConfiguration.menuName')" header-align="center" show-overflow-tooltip>
					<template #default="scope">
						<SvgIcon :name="scope.row.icon" />
						<span class="ml10">{{ $t(scope.row.title) }}</span>
					</template>
				</el-table-column>
				<el-table-column :label="$t('message.pageConfiguration.type')" width="70" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="warning" v-if="scope.row.type === 1">{{$t('message.pageConfiguration.directory')}}</el-tag>
						<el-tag v-else-if="scope.row.type === 2">{{$t('message.pageConfiguration.menu')}}</el-tag>
						<el-tag type="info" v-else>{{$t('message.pageConfiguration.btn')}}</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="path" :label="$t('message.pageConfiguration.pathName')" header-align="center" show-overflow-tooltip />
				<el-table-column prop="component" :label="$t('message.pageConfiguration.component')" align="center" show-overflow-tooltip />
				<el-table-column prop="permission" :label="$t('message.pageConfiguration.perms')" align="center" show-overflow-tooltip />
				<el-table-column prop="orderNo" :label="$t('message.pageConfiguration.sort')" width="70" align="center" show-overflow-tooltip />
				<el-table-column :label="$t('message.pageConfiguration.status')" width="80" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status === 1">{{$t('message.pageConfiguration.enable')}}</el-tag>
						<el-tag type="danger" v-else>{{$t('message.pageConfiguration.disable')}}</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" :label="$t('message.pageConfiguration.createTime')" align="center" show-overflow-tooltip />
				<el-table-column :label="$t('message.pageConfiguration.operate')" width="140" fixed="right" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-button icon="ele-Edit" size="small" text type="primary" @click="openEditMenu(scope.row)" v-auth="'sysMenu:update'"> {{ $t('message.pageConfiguration.edit') }} </el-button>
						<el-button icon="ele-Delete" size="small" text type="danger" @click="delMenu(scope.row)" v-auth="'sysMenu:delete'"> {{ $t('message.pageConfiguration.del') }} </el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>

		<EditMenu ref="editMenuRef" :title="state.editMenuTitle" :menuData="state.menuData" @handleQuery="handleQuery" />
	</div>
</template>

<script lang="ts" setup name="sysMenu">
import { onMounted, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EditMenu from '/@/views/system/menu/component/editMenu.vue';

import { getAPI } from '/@/utils/axios-utils';
import { SysMenuApi } from '/@/api-services/api';
import { SysMenu } from '/@/api-services/models';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();

const editMenuRef = ref<InstanceType<typeof EditMenu>>();
const state = reactive({
	loading: false,
	menuData: [] as Array<SysMenu>,
	queryParams: {
		title: undefined,
		type: undefined,
	},
	editMenuTitle: '',
});

onMounted(async () => {
	handleQuery();
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	var res = await getAPI(SysMenuApi).apiSysMenuListGet(state.queryParams.title, state.queryParams.type);
	state.menuData = res.data.result ?? [];
	state.loading = false;
};

// 重置操作
const resetQuery = () => {
	state.queryParams.title = undefined;
	state.queryParams.type = undefined;
	handleQuery();
};

// 打开新增页面
const openAddMenu = () => {
	state.editMenuTitle = t('message.pageConfiguration.addMenu');
	editMenuRef.value?.openDialog({ type: 2, isHide: false, isKeepAlive: true, isAffix: false, isIframe: false, status: 1, orderNo: 100 });
};

// 打开编辑页面
const openEditMenu = (row: any) => {
	state.editMenuTitle = t('message.pageConfiguration.editMeun');
	editMenuRef.value?.openDialog(row);
};

// 删除当前行
const delMenu = (row: any) => {
	ElMessageBox.confirm(t('message.messages.confirmDelMenu')+`：【${row.title}】?`, t('message.messages.hint'), {
		confirmButtonText: t('message.pageConfiguration.confirm'),
		cancelButtonText: t('message.pageConfiguration.cancel'),
		type: 'warning',
	})
		.then(async () => {
			await getAPI(SysMenuApi).apiSysMenuDeletePost({ id: row.id });
			handleQuery();
			ElMessage.success(t('message.messages.deleteSuccess'));
		})
		.catch(() => {});
};
</script>
